package simulaco.web;

import gminet.infra.db.PoolManager;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import simulaco.web.action.WebAction;

import com.sun.faces.config.ConfigureListener;

public class WebServletListener extends ConfigureListener {

    private static final String CONFIG_FILE_NAME = "configsimulaco.properties";
    
    public void contextInitialized(ServletContextEvent contextEvent) {

        try {
            if (!PoolManager.initialized()) {
                ServletContext context = contextEvent.getServletContext();
                String completeFilePath = context.getRealPath("/WEB-INF");
                PoolManager.initialize(completeFilePath + File.separator + CONFIG_FILE_NAME);
            }
            if (!WebAction.contentRealPathInitialized()) {
                ServletContext context = contextEvent.getServletContext();
                WebAction.inicializarContentRealPath(context.getRealPath("/"));
            }
        } catch (Throwable throwable) {
            System.out.println(">>> ERRO INICIANDO O CONTEXTO SIMULACO....");
            throwable.printStackTrace();
        }
    }
    
    public void contextDestroyed(ServletContextEvent context) {
        PoolManager.destroy();
    }
    
}
